<?php
/**
 * Created by PhpStorm.
 * User: Hello pan
 * Date: 9/19/25
 * Time: 5:28 PM
 */

namespace app\controllers;


use app\models\Customer;

class ElasticSearchController extends \yii\web\Controller
{

    public function actionIndex()
    {
        // 创建索引
        Customer::createIndex();

        // 删除索引
//        Customer::deleteIndex();

        return 1111;
    }

    public function actionAdd()
    {
        $customer = new Customer();

//        $customer->_id = 1; // setting primary keys is only allowed for new records
//        $customer->id = 1; // setting primary keys is only allowed for new records
//        $customer->last_name = 'Doe'; // attributes can be set one by one
//        $customer->first_name = 'Jane';// ['first_name' => 'Jane', 'email' => 'janedoe@example.com']; // or together
//        $customer->order_ids = '1';
//        $customer->email = 'janedoe@example.com';
//        $customer->registered_at =  date('Y-m-d H:i:s');
//        $customer->status = '1';
//        $customer->is_active = true;
//        $customer->updated_at = date('Y-m-d H:i:s');
//        $res = $customer->save();

        $batch_data = [];
        for ($i = 10; $i < 100; $i++) {

            $batch_data[] = [
                'id' => $i,
                'first_name' => '潘'.$i,
                'last_name' => '哈哈',
                'order_ids' => strval($i),
                'email' => "123{$i}@qq.com",
                'status' => '1',
                'is_active' => true,
                'registered_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }


        $res = $customer->batchInsert($batch_data);
    p($res);


        return 2222;
    }

    public function actionQuery()
    {
        // 查询单条
        //$customer = Customer::get(1);
        // 查询单条
        //$customer = Customer::findOne(1);

        // 批量查询,返回数组
        // $list = Customer::find()->asArray()->all();

        // 批量查询,增加条件返回对象
//        $list = Customer::find()->query([
//            'match' => [
//                'first_name' => '潘'
//            ],
//        ])->highlight([
//            'fields' => ['first_name' => (object)[]],
//            'pre_tags' => ['<em>'],
//            'post_tags' => ['</em>']
//        ])->source(['first_name'])
//            ->offset(0)
//            ->limit(3)
//            ->orderBy('id desc')
//            ->fields(['first_name'])
//            ->asArray()
//            ->all();


        // 精准查询
        $list = Customer::find()->query([
//            'term' => [
//                'status' => '1'
//            ],

            'bool' => [
                'must' => [
                    ['match' => ['first_name' => '潘']],
                    ['range' => [
                        'id' => [
                            'gt' => 10,
                            'lt' => 100
                        ]
                    ]],
                    ['match' => [
                        'status' => [
                        'query' => '1',
                        'operator' => 'and'
                    ]]]
                ]
            ]
        ])->limit(30)
            ->asArray()
            ->all();



        p($list);die;
    }

}